package com.coincalf.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.coincalf.dto.CoinInfoDto;
import com.coincalf.entity.Coin;
import com.coincalf.framework.exception.GlobalDefaultException;
import com.coincalf.mapper.CoinMapper;
import com.coincalf.service.CoinService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 币种配置信息 服务实现类
 * </p>
 *
 * @author qiang
 * @since 2018-05-12
 */
@Slf4j
@Service
public class CoinServiceImpl extends ServiceImpl<CoinMapper, Coin> implements CoinService {


    @Autowired
    private CoinMapper coinMapper;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;


    /**
     * 根据主键查询币种信息
     *
     * @param coinId 币种ID
     * @return
     */
    @Override
    public Coin queryById(long coinId) {
        String redisCoinKey = "coinInfo:" + coinId;
        Coin coin = (Coin) redisTemplate.opsForValue().get(redisCoinKey);
        if (coin != null) {
            return coin;
        }
        coin = super.selectOne(new EntityWrapper<Coin>().eq("id", coinId));
        redisTemplate.opsForValue().set(redisCoinKey, coin, 2, TimeUnit.MINUTES);
        return coin;
    }

    /**
     * 根据类型查询币种信息
     *
     * @param type 币种类型
     * @return
     */
    @Override
    public Coin queryByType(String type) {
        EntityWrapper<Coin> wrapper = new EntityWrapper<>();
        wrapper.eq("type", type);
        return super.selectOne(wrapper);
    }

    @Override
    public String queryServerUrl(String coinName) {
        if (StringUtils.isBlank(coinName)) {
            log.warn("币种名称不能为空");
            throw new GlobalDefaultException(-4);
        }
        return baseMapper.selectServerUrl(coinName);
    }

    @Override
    public CoinInfoDto queryInfo(String coinName) {
        if (StringUtils.isBlank(coinName)) {
            log.warn("币种名称不能为空");
            return null;
        }
        Coin coin = baseMapper.selectInfo(coinName);
        if (coin == null) {
            throw new GlobalDefaultException(50058);
        }
        return new CoinInfoDto(coin.getIsMain(), coin.getId(), coin.getName(), coin.getContractAddress(), coin.getMainCoinId());
    }

    @Override
    public List<Coin> queryMainCoins() {
        EntityWrapper<Coin> wrapper = new EntityWrapper<>();
        wrapper.eq("is_main", 1);
        return super.selectList(wrapper);
    }

    @Override
    public List<CoinInfoDto> selectContractAddressesByMainCoinId(Long mainCoinId) {
        return coinMapper.selectContractAddressesByMainCoinId(mainCoinId);
    }

    @Override
    public Long selectMainCoinIdByCoinId(Long coinId) {
        return coinMapper.selectMainCoinIdByCoinId(coinId);
    }

    @Override
    public String selectCoinNameByCoinId(Long coinId) {
        return coinMapper.selectCoinNameByCoinId(coinId);
    }

}
